﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.WishlistUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import org.aswing.*;
    import Classes.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import Display.InventoryUI.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;

    public class WishlistView extends JPanel {

        public static const MAX_SLOTS:int = 5;
        public static const COUNT_HEIGHT:int = 5;
        public static const GRID_MARGIN:int = 5;
        public static const SLOT_WIDTH:int = 94;
        public static const SLOT_HEIGHT:int = 99;
        public static const TOTAL_WIDTH:int = 480;
        public static const SHARE_BUTTON_WIDTH:int = 80;
        public static const SHARE_BUTTON_HEIGHT:int = 40;
        public static const WISHLIST_TITLE_FONT_SIZE:int = 18;

        protected var m_gridList:GridList;
        protected var m_cellFactory:GridListCellFactory;
        protected var m_model:VectorListModel;
        protected var m_blankItems:Array;
        protected var m_inventoryLimitLabel:JLabel;
        protected var m_inventoryLimitPanel:JPanel;
        protected var m_bShowInventoryLimit:Boolean;

        public function WishlistView(_arg1:Boolean=true, _arg2:LayoutManager=null){
            this.m_blankItems = new Array();
            super(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 5, SoftBoxLayout.TOP));
            this.m_bShowInventoryLimit = _arg1;
            var _local3:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            _local3.appendAll(this.makeLeftPane(), this.makeRightPane());
            this.append(_local3);
            this.append(this.makeWishlistPane());
            Global.stage.addEventListener(DataItemEvent.WISHLIST_CHANGED, this.onWishlistChanged, false, 0, true);
            ASwingHelper.prepare(this);
        }
        protected function makeLeftPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER, 5);
            var _local2:AssetPane = new AssetPane((new (InventoryView.assetDict["wishlist_icon"])() as DisplayObject));
            var _local3:JLabel = ASwingHelper.makeLabel(ZLoc.t("Dialogs", "WishlistTitle"), EmbeddedArt.TITLE_FONT, WISHLIST_TITLE_FONT_SIZE);
            _local1.appendAll(_local2, _local3, ASwingHelper.horizontalStrut(5));
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function makeWishlistPane():JPanel{
            var _local4:Item;
            var _local5:int;
            var _local6:int;
            var _local7:XML;
            var _local8:Item;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            this.m_model = new VectorListModel();
            this.m_model.clear();
            var _local2:Array = Global.player.wishlist;
            var _local3:int;
            while (_local3 < MAX_SLOTS) {
                if (_local3 < _local2.length){
                    _local4 = Global.gameSettings.getItemByName(_local2[_local3]);
                    this.m_model.append(_local4);
                };
                _local3++;
            };
            if (_local2.length < MAX_SLOTS){
                _local5 = (MAX_SLOTS - _local2.length);
                _local6 = 0;
                while (_local6 < _local5) {
                    _local7 = XML("<item name='blankItem' />");
                    _local8 = new Item(_local7);
                    this.m_model.append(_local8);
                    this.m_blankItems.push(_local8);
                    _local6++;
                };
            };
            this.m_cellFactory = new WishlistCellFactory(new IntDimension(SLOT_WIDTH, SLOT_HEIGHT));
            this.m_gridList = new GridList(this.m_model, this.m_cellFactory, MAX_SLOTS, 1);
            this.m_gridList.setPreferredHeight(SLOT_HEIGHT);
            this.m_gridList.setPreferredWidth(TOTAL_WIDTH);
            _local1.appendAll(ASwingHelper.horizontalStrut(GRID_MARGIN), this.m_gridList, ASwingHelper.horizontalStrut(GRID_MARGIN));
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function makeRightPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            var _local2:Number = TextFieldUtil.getLocaleFontSize(18, 14, [{
                locale:"es",
                size:12
            }]);
            var _local3:CustomButton = new CustomButton(ZLoc.t("Dialogs", "WishlistShare"), null, "GreenButtonUI");
            _local3.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, _local2));
            _local3.setForeground(new ASColor(0xFFFFFF));
            _local3.setMargin(new Insets(2, 5, 2, 5));
            _local3.filters = [new GlowFilter(33724, 1, 0, 0, 2.5)];
            _local3.addEventListener(MouseEvent.MOUSE_UP, this.onShare, false, 0, true);
            _local3.useHandCursor = true;
            if (this.m_bShowInventoryLimit){
                this.m_inventoryLimitPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
                this.m_inventoryLimitLabel = this.makeInventoryLimitLabel();
                this.m_inventoryLimitPanel.append(this.m_inventoryLimitLabel);
            };
            _local1.appendAll(_local3, ASwingHelper.horizontalStrut(40), this.m_inventoryLimitPanel);
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function makeInventoryLimitLabel():JLabel{
            var _local1:int = Global.player.inventory.capacity;
            var _local2:int = Global.player.inventory.totalCount;
            var _local3:String = ZLoc.t("Dialogs", "InventoryLimit", {
                numItems:_local2,
                totalLimit:_local1
            });
            var _local4:JLabel = ASwingHelper.makeLabel(_local3, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, EmbeddedArt.whiteTextColor);
            return (_local4);
        }
        public function refreshInventoryLimit():void{
            this.m_inventoryLimitPanel.remove(this.m_inventoryLimitLabel);
            this.m_inventoryLimitLabel = this.makeInventoryLimitLabel();
            this.m_inventoryLimitPanel.append(this.m_inventoryLimitLabel);
        }
        protected function onShare(_arg1:MouseEvent):void{
            StatsManager.count("Share", "wishList");
            if (Global.player.wishlist.length > 0){
                Global.world.viralMgr.sendWishlistRequest(Global.player);
            };
        }
        protected function onWishlistChanged(_arg1:DataItemEvent):void{
            var _local2:XML;
            var _local3:Item;
            if ((this.m_model.getSize() - this.m_blankItems.length) < Global.player.wishlist.length){
                this.m_model.removeElementAt((Global.player.wishlist.length - 1));
                this.m_model.insertElementAt(_arg1.item, (Global.player.wishlist.length - 1));
                this.m_blankItems.shift();
            } else {
                if (this.m_model.getSize() > Global.player.wishlist.length){
                    this.m_model.remove(_arg1.item);
                    _local2 = XML("<item name='blankItem' />");
                    _local3 = new Item(_local2);
                    this.m_blankItems.push(_local3);
                    this.m_model.append(_local3);
                };
            };
        }

    }
}//package Display.WishlistUI 
